package com.fuda.trade.ui.orders

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.fuda.trade.MyApplication
import com.fuda.trade.model.TradeOrderData
import com.fuda.trade.R
import com.fuda.trade.ZenohManager
import com.fuda.trade.model.TradeOrder

class OrdersAdapter : ListAdapter<TradeOrder, OrdersAdapter.OrderViewHolder>(OrderDiffCallback()) {

    companion object {
        val TAG: String = OrdersAdapter::class.java.simpleName
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OrderViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val itemView = when (viewType) {
            TradeOrderData.Buy::class.hashCode() -> inflater.inflate(R.layout.item_order_buy, parent, false)
            TradeOrderData.Sell::class.hashCode() -> inflater.inflate(R.layout.item_order_sell, parent, false)
            else -> throw IllegalArgumentException("Invalid view type")
        }
        return OrderViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: OrderViewHolder, position: Int) {
        val order = getItem(position)
        holder.bind(order)
    }

    override fun getItemViewType(position: Int): Int {
        val tradeOrder: TradeOrder = getItem(position)
        return when (tradeOrder.data) {
            is TradeOrderData.Buy -> TradeOrderData.Buy::class.hashCode()
            is TradeOrderData.Sell -> TradeOrderData.Sell::class.hashCode()
            else -> throw IllegalArgumentException("Invalid view type")
        }
    }

    inner class OrderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private lateinit var description: TextView
        private lateinit var confirmButton: Button

        fun bind(tradeOrder: TradeOrder) {
            description = itemView.findViewById(R.id.description)
            description.text = TradeOrder.serialize(tradeOrder)

            confirmButton = itemView.findViewById(R.id.command_confirm) // Adjust ID based on tradeOrderData type
            confirmButton.setOnClickListener {
                onCommandConfirmClicked(tradeOrder, confirmButton)
            }
        }
    }

    private fun onCommandConfirmClicked(tradeOrder: TradeOrder, button: Button) {
        // Handle the tradeOrderData confirmation based on the tradeOrderData type
        when (val data = tradeOrder.data) {
            is TradeOrderData.Buy -> handleBuyConfirm(data, button)
            is TradeOrderData.Sell -> handleSellConfirm(data, button)
            else -> throw IllegalArgumentException("Unknown data type")

        }
    }

    private fun handleBuyConfirm(tradeOrderData: TradeOrderData.Buy, button: Button) {
        // Handle the Buy tradeOrderData confirmation
        // Example: Log the tradeOrderData
        Log.d(TAG,"Buy confirmed: $tradeOrderData")

        tradeOrderData.isAuto = true // set to true to ask TradeAccessibilityService to operate party3 app

        val payload = TradeOrderData.serialize(tradeOrderData)
        MyApplication.zenohManager.publish(ZenohManager.ordersKeyExpr!!, payload)
        MyApplication.zenohManager.publish(ZenohManager.responseKeyExpr!!, payload)

    }

    private fun handleSellConfirm(tradeOrderData: TradeOrderData.Sell, button: Button) {
        // Handle the Sell tradeOrderData confirmation
        // Example: Log the tradeOrderData
        Log.d(TAG,"Sell confirmed: $tradeOrderData")

        tradeOrderData.isAuto = true // set to true to ask TradeAccessibilityService to operate party3 app

        val payload = TradeOrderData.serialize(tradeOrderData)
        MyApplication.zenohManager.publish(ZenohManager.ordersKeyExpr!!, payload)
        MyApplication.zenohManager.publish(ZenohManager.responseKeyExpr!!, payload)

    }

    
    class OrderDiffCallback : DiffUtil.ItemCallback<TradeOrder>() {
        override fun areItemsTheSame(oldItem: TradeOrder, newItem: TradeOrder): Boolean {
            return oldItem::class == newItem::class && oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: TradeOrder, newItem: TradeOrder): Boolean {
            return oldItem == newItem
        }
    }
}